import time

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse


# Create your views here.

def lesson3(request):
    # 目的就是检测请求头参数顺序
    request_header_s = list(request.META.keys())
    standard_s = ['HTTP_HOST', 'HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE']

    print("---", list(request.META.keys()))
    for standard_header in standard_s:
        if standard_header in request_header_s:
            print(standard_header, "++++++++++")
            request_header_s = request_header_s[request_header_s.index(standard_header):]
        else:
            print(standard_header, "-----")
            # return HttpResponse("你的爬虫被识别,警告!")
            return render(request, "sec1/lesson1_1.html", {"my_name": "你的爬虫被识别,警告!"})

    # return HttpResponse("success")
    return render(request, "sec1/lesson1_1.html", {"my_name": "success"})


def lesson5(request):
    return render(request, "sec1/lesson5.html")

def lesson6(request):
    return render(request, "sec1/lesson6.html")


def lesson4(request):
    return render(request, "sec1/lesson4.html")


# def lesson5_data_api(request):
#     data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#     return JsonResponse({"status": 200, "data": data}, status=200)

def lesson5_data_api(request):
    token_list = request.GET.get("token").split("|")
    _sum = token_list[1]
    _time = token_list[0]
    _check_sum = 0
    for _number in _time:
        _check_sum += int(_number)
        _check_sum +=5
    _time_ = int(int(_time) / 1000)
    request_header_s = list(request.META.keys())
    standard_s = ['HTTP_HOST',"HTTP_USER_AGENT"]

    print("---", list(request.META.keys()))
    for standard_header in standard_s:
        if standard_header in request_header_s:
            print(standard_header, "++++++++++")
            request_header_s = request_header_s[request_header_s.index(standard_header):]
        else:
            # print(standard_header, "-----")
            return JsonResponse({"status": 200, "data": ["error"]}, status=403)
    print("_time***", _time_)
    now_time = int(time.time())
    print("now_time---", now_time)
    if int(_sum) == _check_sum and now_time - _time_ < 30:
        data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        return JsonResponse({"status": 200, "data": data}, status=200)
    else:
        return JsonResponse({"status": 403, "data": []}, status=403)

def lesson6_data_api(request):
    token = request.GET.get("token")
    return HttpResponse(request.environ.get("ssl_ciphers"))
    token = request.GET.get("token")
    print(token)
    # print("---",token)
    # decode_result = int(''.join([chr(ord(i) - 1314) for i in token]))
    # # print("decode_result***",decode_result)
    # if abs(decode_result/1000 - time.time()) <= 60:
    #     data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    #     return JsonResponse({"status": 200, "data": data}, status=200)
    # else:
    #     return JsonResponse({"status": 200, "data": []}, status=403)
